// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-16 16:54
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.ArraysTrain;

import java.util.HashMap;
import java.util.Map;

public class ContainsNearbyDuplicate {
    /**
     * 判断相同元素区间大小
     * HashMap -> 记录上次当前元素出现的位置（最近）
     */
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> data = new HashMap<Integer, Integer>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            int prev = data.getOrDefault(nums[i], i);
            data.put(nums[i], i);
            if (i - prev > 0 && i - prev <= k) {
                return true;
            }
        }
        return false;
    }
}
